/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/*
 * Function input type is number
 */
function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 47 || charCode > 58)) {
        return false;
    }
    return true;
}

/*
 * Function generate captcha/security field
 */
function generate_captcha(){
    var qwe = Math.floor((Math.random() * 10) + 1);
    var ewq = Math.floor((Math.random() * 10) + 1);

    $(".qwe").val(qwe);
    $(".ewq").val(ewq);
    $(".checking_human").html("Are you Human? " + qwe + " + " + ewq + " = ");
}

/*
 * Function clear form/reset form
 */
function clearformall(ele) {
    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
            case 'email':
            case 'number':
                $(this).val(''); break;
            case 'checkbox':
            case 'radio':
                this.checked = false; break;
        }
	});
    
    $(ele).find('.info').css('display','none');
}

$(document).ready(function(){
    // Set Month Array
    var month           = ['Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','Nopember','Desember'];
    // Set Current Month
    var current_month   = parseInt ( (new Date).getMonth() );
    // Get This Month
    var this_month      = month[current_month];
    
    // Generate Captcha
    generate_captcha();
    
    // Load E-Library Data
    _load_elibrary();
    // Load Events Data
    _load_event();
    // Load News Data
    _load_news();
    // Load Fundrising/Product Data
    _load_product();
    // Load Images Gallery
    _load_images();
    // Load Cart List
    _load_cartlist();
    
    // Reset Register Form
    $("#button-register-reset").click(function () {
        var reg_form = $("#register-form");
        clearformall(reg_form);
        $("#messages").hide();
        return false;
    });
    
    // Reset Login Form
    $("#button-login-reset").click(function () {
        var log_form = $("#login-form");
        clearformall(log_form);
        $("#messages").hide();
        return false;
    });
    
    // Select AJAX State
    load_propinsi($("#korban_propinsi"), $("select#korban_kota"), $("#korban_kota_info"));
    load_propinsi($("#korban_propinsi2"), $("select#korban_kota2"), $("#korban_kota2_info"));
    load_propinsi($("#dk_propinsi"), $("select#dk_kota"), $("#dk_kota_info"));
    load_propinsi($("#dk_propinsi2"), $("select#dk_kota2"), $("#dk_kota2_info"));
    load_propinsi($("#saksi_propinsi"), $("select#saksi_kota"), $("#saksi_kota_info"));
    load_propinsi($("#co_propinsi"), $("select#co_kota"), $("#co_kota_info"));
    load_propinsi($("#cos_propinsi"), $("select#cos_kota"), $("#cos_kota_info"));
    
    // Select DOB
    load_dob($("#korban_tgllahir"), $("#korban_tgllahir_info"));
    load_dob($("#korban_tgllahir2"), $("#korban_tgllahir2_info"));
    load_dob($("#saksi_tgllahir"), $("#saksi_tgllahir_info"));
    load_dob($("#donasi_conf_tgl"), $("#donasi_conf_tgl_info"));
    
    // Select Date Picker
    load_date($('#don_tanggal'));
    
    // Select AJAX Hubungan
    load_hubungan($('#pelaku_hub_kat'),  $("select#pelaku_hub"), $('#pelaku_hub_info'));
    load_hubungan($('#pelaku_hub_kat2'),  $("select#pelaku_hub2"), $('#pelaku_hub2_info'));
    load_hubungan($('#saksi_hub_kat'),  $("select#saksi_hub"), $('#saksi_hub_info'));
    
    // Load Propinsi Function
    function load_propinsi(el, el_city, el_city_info){
        el.change(function() {
            var stateid = $(this).val();
            var myData  = 'stateid='+stateid.toLowerCase(); // Build a post data structure
            var url     = 'city/get_city';
            
            jQuery.ajax({
                type:       "POST",     // HTTP method POST or GET
                url:        url,        // Where to make Ajax calls
                dataType:   "text",     // Data type, HTML, json etc.
                data:       myData,     // Form variables
                beforeSend: function(){
                    $("div#mask").fadeIn();
                },
                success:function(response){
                    var obj = jQuery.parseJSON(response);
                    
                    if(obj.status === 1){
                        el_city.empty();
                        $.each(obj.list, function(i, item) {
                            el_city.append('<option value="'+obj.list[i].city_id+'">'+obj.list[i].city_name+'</option>')
                        });
                        el_city_info.fadeOut();
                    }else if(obj.status == '0'){
                        el_city.html('<option value="">Pilih Kota / Kabupaten</option>').change();
                        $("div#messages").removeClass("n_ok").addClass("n_error");
                        $("div#messages").html("<p>"+obj.msg+"</p>");
                        $("div#messages").fadeIn();$("#downline").focus();
                        $("div#loading").hide(); 
                        $("div#bg-loading").hide();$("#downline").focus();
                    }
                    $("div#mask").fadeOut();
                },
                error:function (xhr, ajaxOptions, thrownError){
                    $("div#loading").hide();
                    $("div#bg-loading").hide();
                    alert(thrownError);
                }
            });
        });
    }
    
    // Load Hubungan Function
    function load_hubungan(el, el_hub, el_hub_info){
        el.change(function() {            
            var hubid   = $(this).val();
            var hub1    = [
                {'hub_val':'suami', 'hub_text':'Suami'}, 
                {'hub_val':'istri', 'hub_text':'Istri'}, 
                {'hub_val':'ayah',  'hub_text':'Ayah'}, 
                {'hub_val':'ibu',   'hub_text':'Ibu'}, 
                {'hub_val':'kakak', 'hub_text':'Kakak'}, 
                {'hub_val':'adik',  'hub_text':'Adik'}, 
                {'hub_val':'paman', 'hub_text':'Paman'}, 
                {'hub_val':'bibi',  'hub_text':'Bibi'},
                {'hub_val':'kakek', 'hub_text':'Kakek'},
                {'hub_val':'nenek', 'hub_text':'Nenek'},
                {'hub_val':'anak',  'hub_text':'Anak'}
            ];
            var hub2    = [{'hub_val':'tetangga','hub_text':'Tetangga'},{'hub_val':'teman','hub_text':'Teman'},{'hub_val':'pacar','hub_text':'Pacar'}];
            var hub3    = [{'hub_val':'orang lain', 'hub_text':'Orang Lain'}];
            
            el_hub.empty();
            if(hubid == 1){
                $.each(hub1, function(i, item) { el_hub.append('<option value="' + hub1[i].hub_val + '">' + hub1[i].hub_text + '</option>'); });
                el_hub_info.fadeOut();
            }else if(hubid == 2){
                $.each(hub2, function(i, item) { el_hub.append('<option value="' + hub2[i].hub_val + '">' + hub2[i].hub_text + '</option>'); });
                el_hub_info.fadeOut();
            }else if(hubid == 3){
                $.each(hub3, function(i, item) { el_hub.append('<option value="' + hub3[i].hub_val + '">' + hub3[i].hub_text + '</option>'); });
                el_hub_info.fadeOut();
            }else{
                el_hub.html('<option value="">Pilih Hubungan</option>').change();
            }
        });
    }
    
    // Load Date of Birth Function
    function load_dob(el, el_info){
        el.datepicker({
            changeMonth:    true,
            changeYear:     true,
            dateFormat:     "dd-mm-yy",
            yearRange:      '1920:2020',
            onSelect: function() {
                el_info.fadeOut();
            },
    	});
    }
    
    // Load Date Picker
    function load_date(el){
        el.datepicker({
            changeMonth:    true,
            changeYear:     true,
            dateFormat:     "yy-mm-dd",
    	});
    }
    
    // Reset Case Tab Form
    function reset_tab_form(el){
        if(el == 1){
            $('#wrapper-report2, #wrapper-report3').hide();
            $('#wrapper-report1').show().fadeIn();
        }else{
            $('#wrapper-report-saksi2, #wrapper-report-saksi3, #wrapper-report-saksi4').hide();
            $('#wrapper-report-saksi1').show().fadeIn();
        }
    }
    
    // Event Tab Korban Form Click
    $('#tab-korban-form').click(function (e){        
        e.preventDefault();
        clearformall($("#korban-report-form"));
        reset_tab_form(1);
    });
    
    // Event Tab Saksi Click
    $('#tab-saksi-form').click(function (e){        
        e.preventDefault();
        clearformall($("#saksi-report-form"));
        reset_tab_form(0);
    });
    
    // Event Tab Register Form Click
    $('#tab-register-form').click(function (e){        
        e.preventDefault();
        clearformall($("#register-form"));
    });
    
    // Event Tab Login Click
    $('#tab-login-form').click(function (e){        
        e.preventDefault();
        clearformall($("#login-form"));
    });
    
    // Event Colorbox Responsive Size Setting
    var cboxOptions = {
        width: '85%',
        height: '85%',
        maxWidth: '80%',
        maxHeight: '80%',
    }
    $('.cbox-link').colorbox(cboxOptions);
    $(window).resize(function(){
        $.colorbox.resize({
          width: window.innerWidth > parseInt(cboxOptions.maxWidth) ? cboxOptions.maxWidth : cboxOptions.width,
          height: window.innerHeight > parseInt(cboxOptions.maxHeight) ? cboxOptions.maxHeight : cboxOptions.height
        });
    });

    // E-Library Link Download
    $('a.elibrary-dl').each(function(){
        $(this).click(function(e){
            var href = $('a#elibrary').attr('href');
            e.preventDefault();
            window.open(href);
        });
    });
    
    // Video Details
    $(".video_details").each(function(){
        $(this).click(function(){
            var url     = $(this).attr('href');
            var id      = $(this).attr('id');
            
            $.ajax({
                type:   "GET",
                data:   '',
                url:    url,
                beforeSend: function (){
                    $("div#mask").fadeIn();
                },
                success: function( response ){
                    $('html, body').animate({ scrollTop: $(".featured-video").offset().top + 110}, 1000);
                    response = $.parseJSON(response);
                    
                    if(response.video != ''){
                        $('#video_details').hide().empty();
                        $('#video_details').html(response.video).fadeIn('slow');
                        $("div#mask").fadeOut();
                        
                        // Close Video Details
                        $('a.video_close').click(function(){
                            $('html, body').animate({ scrollTop: $(".featured-video").offset().top - 135}, 1000);
                            $('#video_details').fadeOut();
                            if(response.source == true){
                                $(".video-js")[0].player.pause();
                            }
                            return false;
                        });
                        
                        if(response.source == true){
                            videojs(document.getElementById("video_det"), {}, function() {
                                this.load();
                            });
                            
                            // Unless using the CDN hosted version, update the URL to the Flash SWF
                            videojs.options.flash.swf = "<?php echo site_url()?>assets/extends/videojs/video-js.swf";   
                        }
                    }
                    return false;
                }
            });
            return false;
        });
    });
    
    // Event Click Button Register
    $('#btn-register').click(function(){
        $('html, body').animate({ scrollTop: $("#member").offset().top}, 1000);
        $('#member').find('.nav-tabs > li:first-child').addClass('active');
        $('#member').find('.nav-tabs > li:last-child').removeClass('active');
        $('#member').find('.tab-content > li > #register').addClass('active in').show().css('height','auto');
        $('#member').find('.tab-content > li > #login').removeClass('active in').hide().css('height','0');
        return false;
    });
    
    // Event to Login
    $('#btn-login, #btn-donation, #btn-case-login').click(function(){
        $('html, body').animate({ scrollTop: $("#member").offset().top}, 1000);
        $('#member').find('.nav-tabs > li:first-child').removeClass('active');
        $('#member').find('.nav-tabs > li:last-child').addClass('active');
        $('#member').find('.tab-content > li > #register').removeClass('active in').hide().css('height','0');
        $('#member').find('.tab-content > li > #login').addClass('active in').show().css('height','auto');
        return false;
    });
    
    // Load Event Data
    // ===============================================================================
    function _load_event( $filter, $type, url ){
        url = typeof(url) != 'undefined' ? url : 'event/event_data';
        el  = $("#event_list");
        pag = $('#pagination_events');
        
        $.ajax({
            type: "GET",
            data: {
                'filter_by'     : $type,
                'filter_val'    : $filter,
                'search_val'    : $("#search_event").val(),
            },
            url: url,
            beforeSend: function (){
                $("div#mask").fadeIn();
            },
            success: function( response ){
                response = $.parseJSON(response);
                
                if(response.events == ""){
                    $(el).hide().empty();
                    $("div#mask").fadeOut();
                }else{
                    $(el).hide().empty();
                    $(el).html(response.events).fadeIn('fast');
                    $(pag).hide().empty();
                    $(pag).html(response.pagination_events).fadeIn('fast');
                    $("div#mask").fadeOut();
                    
                    // Event Calendar
                    if(response.calendar != ''){
                        $('.calendar-content').hide().empty();
                        $('.calendar-content').html(response.calendar).fadeIn('fast');
                        
                        // Event Click Event Date
                        $('a.event_date').each(function (){
                            $(this).click(function(){
                                var val     = $(this).find('.event-date-val').val();
                                $('html, body').animate({ scrollTop: $("#events").offset().top}, 500);
                                _load_event( val, 'date' );
                                return false;
                            });
                        });
                    }
                    
                    // Month Archive Active
                    if(response.month != ''){
                        $('a.archive_active').removeClass('archive_active');
                        $("a.event_archive[title='"+ response.month +"']").addClass('archive_active');
                    }
                    
                    // Event Pagination
                    $('.pagination_event').each(function(){
                        $(this).click(function(){
                            $('html, body').animate({ scrollTop: $("#events").offset().top}, 500);
                            _load_event($filter, $type, $(this).attr('href'));
                            return false;
                        });
                    });
                    
                    // Event Colorbox
                    $(".event-details").each(function(){
                        $(this).colorbox({ 
                            width: "80%",
                            height: "80%",
                            opacity: 0.6,
                            onComplete: function(){
                                var elimg   = '.event-image-' + $(this).data("id");
                                $(elimg).flexslider({
                                    animation: "slide",
                                    pauseOnHover: true,
                                    controlNav: false,
                                    directionNav: true,
                                    slideshow: false,
                                    smoothHeight: true,
                                    nextText: "",
                                    prevText: "",
                                    useCSS: false
                                });
                            }
                        });
                    });
                }
                return false;
            }
        });
        return false;
    }
    
    // Scale Slider Event Image                                       
    function ScaleSliderEvent(id) {
        var parentWidth = $('#event_image').parent().parent().width();
        
        if (parentWidth) {
            id.$ScaleWidth(parentWidth - 60);
        }else{
            window.setTimeout(ScaleSlider, 10);
        }
    }
    
    // Event Click Event Archive
    $('a.event_archive').each(function(){
        $(this).click(function(){
            var val     = $(this).attr('title');
            $('html, body').animate({ scrollTop: $("#events").offset().top}, 500);
            _load_event( val, 'archive' );
            
            $('a.archive_active').removeClass('archive_active');
            $(this).addClass('archive_active');
            
            return false;
        });
    });
    
    // Event Click Event Date
    $('a.event_date').each(function (){
        $(this).click(function(){
            $('html, body').animate({ scrollTop: $("#events").offset().top}, 500);
            var val     = $(this).find('.event-date-val').val();
            _load_event( val, 'date' );
            return false;
        });
    });
    
    // Event Click Search Event
    $("#btn_search_event").click(function(){
        $('html, body').animate({ scrollTop: $("#events").offset().top}, 500);
        _load_event();
    });
    // ===============================================================================
    
    
    // Load News Data
    // ===============================================================================
    function _load_news( $filter, $type, url ){
        url     = typeof(url) != 'undefined' ? url : 'news/news_data';
        eln     = $("#news_list");
        pagn    = $('#pagination_news');
        
        $.ajax({
            type: "GET",
            data: {
                'filter_by'     : $type,
                'filter_val'    : $filter,
                'search_val'    : $("#search_news").val(),
            },
            url: url,
            beforeSend: function (){
                $("div#mask").fadeIn();
            },
            success: function( response ){
                response = $.parseJSON(response);
                
                if(response.news == ""){
                    $(eln).hide().empty();
                    $("div#mask").fadeOut();
                }else{
                    $(eln).hide().empty();
                    $(eln).html(response.news).fadeIn('fast');
                    $(pagn).hide().empty();
                    $(pagn).html(response.pagination_news).fadeIn('fast');
                    $("div#mask").fadeOut();
                    
                    // News Pagination
                    $('.pagination_news').each(function(){
                        $(this).click(function(){
                            $('html, body').animate({ scrollTop: $("#news").offset().top}, 500);
                            _load_news($filter, $type, $(this).attr('href'));
                            return false;
                        });
                    });
                    
                    // News Colorbox
                    $(".news-details").each(function(){
                        $(this).colorbox({
                            width: "80%",
                            height: "80%",
                            opacity: 0.6,
                            onComplete: function(){
                                var elimg   = '.news-image-' + $(this).data("id");
                                $(elimg).flexslider({
                                    animation: "slide",
                                    pauseOnHover: true,
                                    controlNav: false,
                                    directionNav: true,
                                    slideshow: false,
                                    smoothHeight: true,
                                    nextText: "",
                                    prevText: "",
                                    useCSS: false
                                });
                            }
                        });
                    });
                }
                return false;
            }
        });
        return false;
    }
    
    // Event Click News Archive
    $('a.news_archive').each(function(){
        $(this).click(function(){
            var val     = $(this).attr('title');
            $('html, body').animate({ scrollTop: $("#news").offset().top}, 500);
            _load_news( val, 'archive' );
            return false;
        });
    });
    
    // Event Click Search News
    $("#btn_search_news, #news_showmore").click(function(){
        $('html, body').animate({ scrollTop: $("#news").offset().top}, 500);
        _load_news();
        return false;
    });
    // ===============================================================================
    
    
    // Load E-Library Data
    // ===============================================================================
    function _load_elibrary( url, val ){
        url     = typeof(url) != 'undefined' ? url : 'elibrary/elibrary_data';
        ele     = $("#elibrary_list");
        page    = $('#pagination_elibrary');
        
        $.ajax({
            type: "GET",
            data: {
                'search_val'    : $("#search_elibrary").val(),
                'search_fil'    : $("#elibrary_kat").val(),
            },
            url: url,
            beforeSend: function (){
                $("div#mask").fadeIn();
            },
            success: function( response ){
                response = $.parseJSON(response);
                
                if(response.elibrary == ""){
                    $(ele).hide().empty();
                    $("div#mask").fadeOut();
                    $('#elibrary_details').hide().empty();
                }else{
                    $(ele).hide().empty();
                    $(ele).html(response.elibrary).fadeIn('fast');
                    $(page).hide().empty();
                    $(page).html(response.pagination).fadeIn('fast');
                    $("div#mask").fadeOut();
                    $('#elibrary_details').hide().empty();
                    
                    // Pagination
                    $('.pagination_elibrary').each(function(){
                        $(this).click(function(){
                            $('html, body').animate({ scrollTop: $("#elibrary").offset().top}, 500);
                            _load_elibrary( $(this).attr('href') );
                            return false;
                        });
                    });
                    
                    // Event Click Library Download
                    $('a.elibrary').each(function(){
                        $(this).click(function (e){
                            var href = $('a.elibrary').attr('href');
                            e.preventDefault();
                            window.open(href);
                        });
                    });
                    
                    // E-Library Details
                    $('a.lib_det').each(function(){
                        $(this).click(function (e){        
                            e.preventDefault();
                            var id  = parseInt( $(this).attr('id') );
                            
                            $.ajax({
                                type: "POST",
                                data: {
                                    'id_elibrary'   : id,
                                },
                                url: 'elibrary/elibrary_details',
                                beforeSend: function (){
                                    $("div#mask").fadeIn();
                                },
                                success: function( response ){
                                    $('html, body').animate({ scrollTop: $("#pagination_elibrary").offset().top - 97}, 500);
                                    $('#elibrary_details').hide().empty();
                                    $('#elibrary_details').html(response).fadeIn('fast');
                                    $("div#mask").fadeOut();
                                }
                            });
                            return false;
                        }); 
                    });
                    
                }
                return false;
            }
        });
        return false;
    }
    
    // Event Click Search E-Library
    $("#btn_search_elibrary").click(function(){
        $('html, body').animate({ scrollTop: $("#elibrary").offset().top}, 500);
        _load_elibrary();
    });
    
    // Event Change E-Library Filter
    $("#elibrary_kat").change(function(){
        $('html, body').animate({ scrollTop: $("#elibrary").offset().top}, 500);
        _load_elibrary();
    });
        
    // Event Click Read Donation Term
    $('#read_donation_term').click(function(e){
        e.preventDefault();
        var el  = $('#donation_term');
        
        if(el.is(':visible')){
            el.fadeOut();
            $(this).find('span').removeClass('btn-expanded').addClass('btn-collapse');
        }else{
            el.fadeIn();
            $(this).find('span').removeClass('btn-collapse').addClass('btn-expanded');
        }
        return false;
    });
    // ===============================================================================
    
    
    // Load Product Data
    // ===============================================================================
    function _load_product( url ){
        url     = typeof(url) != 'undefined' ? url : 'fundrising/product_data';
        elp     = $("#product_list");
        pagp    = $('#pagination_product');
        
        $.ajax({
            type: "GET",
            data: {
                'search_val'    : $("#search_product").val(),
                'search_fil'    : $("#product_filter").val(),
            },
            url: url,
            beforeSend: function (){
                $("div#mask").fadeIn();
            },
            success: function( response ){
                //alert(response);
                //return false;
                
                response = $.parseJSON(response);
                
                if(response.products == ""){
                    $(elp).hide().empty();
                    $("div#mask").fadeOut();
                }else{
                    $(elp).hide().empty();
                    $(elp).html(response.products).fadeIn('fast');
                    $(pagp).hide().empty();
                    $(pagp).html(response.pagination).fadeIn('fast');
                    $("div#mask").fadeOut();
                    
                    // Pagination
                    $('.pagination_fundrising').each(function(){
                        $(this).click(function(){
                            $('html, body').animate({ scrollTop: $("#fundrising").offset().top}, 500);
                            _load_product( $(this).attr('href') );
                            return false;
                        });
                    });
                    
                    // Product Colorbox
                    $("a.product_details, a.product_details_title").each(function(){
                        $(this).colorbox({
                            width: "80%",
                            height: "80%",
                            opacity: 0.6,
                            onComplete: function(){
                                var elimg   = '.prod-image-' + $(this).data("id");
                                $(elimg).flexslider({
                                    animation: "slide",
                                    pauseOnHover: true,
                                    controlNav: false,
                                    directionNav: true,
                                    slideshow: false,
                                    smoothHeight: true,
                                    nextText: "",
                                    prevText: "",
                                    useCSS: false
                                });
                                
                                // Buy Product
                                $('.btn-buy').each(function(){
                                    $(this).click(function(){
                                        var url_cart    = $(this).attr('href');
                                        var elc         = $("#cart_list");
                                        var elc_item    = $(".shopping-cart-item");
                                        var parent      = $(this).parent().parent();
                                        var qty         = parent.find('.qty');
                                        var message     = parent.parent().parent().find('div.message');
                                        var val         = qty.val();
                                        
                                        if(val == 0){
                                            qty.focus();
                                        }else{
                                            $.ajax({
                                                type: "POST",
                                                data: {
                                                    'qty' : val
                                                },
                                                url: url_cart,
                                                beforeSend: function (){
                                                    $("div#mask").fadeIn();
                                                },
                                                success: function( response ){
                                                    response = $.parseJSON(response);
                                                    $("div#mask").fadeOut();
            
                                                    if(response.message != ""){
                                                        message.empty().html(response.message).fadeIn('fast').delay(5000).fadeOut('slow');
                                                        qty.val(response.stock);
                                                    }else{
                                                        $.fn.colorbox.close();
                                                        qty.val('');
                                                        
                                                        $(elc).hide().empty();
                                                        $(elc).html(response.items);
                                                        $(elc_item).hide().empty();
                                                        $(elc_item).html(response.item_count).fadeIn('fast');
                                                        
                                                        // Remover Cart Item
                                                        $('.remove-item').each(function(){
                                                            var url_rmv = $(this).data('url');
                                                            $(this).click(function(){
                                                                $.ajax({
                                                                    type: "POST",
                                                                    data: {},
                                                                    url: url_rmv,
                                                                    success: function( response ){
                                                                        if(response == "removed"){
                                                                            _load_cartlist(true);
                                                                        }
                                                                        return false;
                                                                    }
                                                                });
                                                            });
                                                        });
                                                        
                                                        // Update Cart Item
                                                        $('.update-item').each(function(){
                                                            var url_upd = $(this).data('url');
                                                            
                                                            $(this).click(function(){
                                                                var qty     = $(this).parent().parent().find('.cart-qty');
                                                                var val     = qty.val();
                                                                $.ajax({
                                                                    type: "POST",
                                                                    data: {
                                                                        'qty' : val
                                                                    },
                                                                    url: url_upd,
                                                                    beforeSend: function (){
                                                                        $('#cart_message').empty();
                                                                        $("div#mask").fadeIn();
                                                                    },
                                                                    success: function( response ){
                                                                        response = $.parseJSON(response);
                                                                        $("div#mask").fadeOut();
                                                                        
                                                                        if(response.update == true){
                                                                            _load_cartlist(true);
                                                                        }else{
                                                                            $('#cart_message').empty().html(response.message).fadeIn('fast').delay(5000).fadeOut('slow');
                                                                            qty.val(response.stock);
                                                                        }
                                                                        return false;
                                                                    }
                                                                });
                                                            });
                                                        });
                                                        
                                                        // Checkout Button
                                                        $("#btn_checkout").click(function(e){
                                                            e.preventDefault();
                                                            $("div#mask").fadeIn().delay(1000).fadeOut(function(){
                                                                $("#shopping_page").hide();
                                                                $("#checkout_page").fadeIn('fast',function(){
                                                                    // Reset Form On Refresh
                                                                    clearformall($("#checkout-form"));
                                                                    $('#co_ship_this').prop("checked", true);
                                                                    $('#shipping_info, #payment_info, #order_review').hide();
                                                                    $('#billing_info').show();
                                                                });
                                                            });
                                                        });
                                                    }
                                                }
                                            });
                                        }
                                        
                                        return false;
                                    });
                                });
                            }
                        });
                    });
                    
                    // Buy Product
                    $('.buy').each(function(){
                        $(this).click(function(){
                            var url_cart    = $(this).attr('href');
                            var elc         = $("#cart_list");
                            var elc_item    = $(".shopping-cart-item");
                            
                            $.ajax({
                                type: "POST",
                                data: {
                                    'qty' : 1
                                },
                                url: url_cart,
                                beforeSend: function (){
                                    $("div#mask").fadeIn();
                                },
                                success: function( response ){
                                    response = $.parseJSON(response);
                                    $("div#mask").fadeOut();
                                    
                                    $(elc).hide().empty();
                                    $(elc).html(response.items);
                                    $(elc_item).hide().empty();
                                    $(elc_item).html(response.item_count).fadeIn('fast');
                                    
                                    // Remover Cart Item
                                    $('.remove-item').each(function(){
                                        var url_rmv = $(this).data('url');
                                        $(this).click(function(){
                                            $.ajax({
                                                type: "POST",
                                                data: {},
                                                url: url_rmv,
                                                success: function( response ){
                                                    if(response == "removed"){
                                                        _load_cartlist(true);
                                                    }
                                                    return false;
                                                }
                                            });
                                        });
                                    });
                                    
                                    // Update Cart Item
                                    $('.update-item').each(function(){
                                        var url_upd = $(this).data('url');
                                        
                                        $(this).click(function(){
                                            var qty     = $(this).parent().parent().find('.cart-qty');
                                            var val     = qty.val();
                                            $.ajax({
                                                type: "POST",
                                                data: {
                                                    'qty' : val
                                                },
                                                url: url_upd,
                                                beforeSend: function (){
                                                    $('#cart_message').empty();
                                                    $("div#mask").fadeIn();
                                                },
                                                success: function( response ){
                                                    response = $.parseJSON(response);
                                                    $("div#mask").fadeOut();
                                                    
                                                    if(response.update == true){
                                                        _load_cartlist(true);
                                                    }else{
                                                        $('#cart_message').empty().html(response.message).fadeIn('fast').delay(5000).fadeOut('slow');
                                                        qty.val(response.stock);
                                                    }
                                                    return false;
                                                }
                                            });
                                        });
                                    });
                                    
                                    // Checkout Button
                                    $("#btn_checkout").click(function(e){
                                        e.preventDefault();
                                        $("div#mask").fadeIn().delay(1000).fadeOut(function(){
                                            $("#shopping_page").hide();
                                            $("#checkout_page").fadeIn('fast',function(){
                                                // Reset Form On Refresh
                                                clearformall($("#checkout-form"));
                                                $('#co_ship_this').prop("checked", true);
                                                $('#shipping_info, #payment_info, #order_review').hide();
                                                $('#billing_info').show();
                                            });
                                        });
                                    });
                                }
                            });
                            return false;
                        });
                    });
                }
                return false;
            }
        });
        return false;
    }
    
    // Scale Slider Event Image                                       
    function ScaleSliderProduct(id) {
        var parentWidth = $('#product_image').parent().width();
        
        if (parentWidth) {
            id.$ScaleWidth(parentWidth - 60);
        }else{
            window.setTimeout(ScaleSliderProduct(id), 10);
        }
    }
    
    // Load Cart List
    function _load_cartlist( show, url ){
        var show        = typeof(show) != 'undefined' ? show : false;
        var url_cart    = typeof(url) != 'undefined' ? url : 'fundrising/cartlist';
        var elc         = $("#cart_list");
        var elc_item    = $(".shopping-cart-item");
        
        $.ajax({
            type: "POST",
            data: {},
            url: url_cart,
            success: function( response ){
                response = $.parseJSON(response);
                
                $(elc).hide().empty();
                if( show ){
                    $(elc).html(response.items).fadeIn('fast');
                }else{
                    $(elc).html(response.items);            
                }
        
                $(elc_item).hide().empty();
                $(elc_item).html(response.item_count).fadeIn('fast');
                
                // Remove Cart Item
                $('.remove-item').each(function(){
                    var url_rmv = $(this).data('url');
                    $(this).click(function(){
                        $.ajax({
                            type: "POST",
                            data: {},
                            url: url_rmv,
                            success: function( response ){
                                if(response == "removed"){
                                    _load_cartlist(true);
                                }
                                return false;
                            }
                        });
                    });
                });
                
                // Update Cart Item
                $('.update-item').each(function(){
                    var url_upd = $(this).data('url');
                    
                    $(this).click(function(){
                        var qty     = $(this).parent().parent().find('.cart-qty');
                        var val     = qty.val();
                        
                        $.ajax({
                            type: "POST",
                            data: {
                                'qty' : val
                            },
                            url: url_upd,
                            beforeSend: function (){
                                $('#cart_message').empty();
                                $("div#mask").fadeIn();
                            },
                            success: function( response ){
                                response = $.parseJSON(response);
                                $("div#mask").fadeOut();
                                
                                if(response.update == true){
                                    _load_cartlist(true);
                                }else{
                                    $('#cart_message').empty().html(response.message).fadeIn('fast').delay(5000).fadeOut('slow');
                                    qty.val(response.stock);
                                }
                                return false;
                            }
                        });
                    });
                });
                
                // Event Click Continue Shopping
                $('#continue-shopping').click(function(){
                    var el  = $('.cart-details');
                    el.fadeOut();
                });
                
                // Checkout Button
                $("#btn_checkout").click(function(e){
                    e.preventDefault();
                    $("div#mask").fadeIn().delay(1000).fadeOut(function(){
                        $("#shopping_page").hide();
                        $("#checkout_page").fadeIn('fast',function(){
                            // Reset Form On Refresh
                            clearformall($("#checkout-form"));
                            $('#co_ship_this').prop("checked", true);
                            $('#shipping_info, #payment_info, #order_review').hide();
                            $('#billing_info').show();
                        });
                    });
                });
                
                return false;
            }
        });
    }
        
    // Event Click Search Products
    $("#btn_search_product").click(function(){
        $('html, body').animate({ scrollTop: $("#fundrising").offset().top}, 500);
        _load_product();
    })
    
    // Event Change Product Filter
    $("#product_filter").change(function(){
        $('html, body').animate({ scrollTop: $("#fundrising").offset().top}, 500);
        _load_product();
    });
    
    // Event Click Shopping Cart
    $('#cart').click(function(e){
        e.preventDefault();
        var el          = $('#cart_list');
        
        if(el.is(':visible')){
            el.fadeOut();
        }else{
            el.fadeIn();
        }
        return false;
    });
    
    // Event Click Confirm Order
    $('#confirm-order').click(function(e){
        e.preventDefault();
        $("div#mask").fadeIn().delay(1000).fadeOut(function(){
            clearformall( $("#order-confirm-form") );
            $('.bill').hide();
            $("#shopping_page").hide();
            $("#order_confirm_page").fadeIn(); 
        });
    });
    
    // Event Click Back to Shopping
    $('#button-shopping').click(function(e){
        e.preventDefault();
        $("div#mask").fadeIn().delay(1000).fadeOut(function(){
            $("#order_confirm_page").hide();
            $("#shopping_page").fadeIn();
            _load_cartlist();
            $('html, body').animate({ scrollTop: $("#fundrising").offset().top}, 500);
        });
    });
    
    // Event Click Back to Shopping
    $('#button-shopping-thanks').click(function(e){
        e.preventDefault();
        $("div#mask").fadeIn().delay(1000).fadeOut(function(){
            $("#thanks_order_page").hide();
            $("#shopping_page").fadeIn();
            _load_cartlist();
            $('html, body').animate({ scrollTop: $("#fundrising").offset().top}, 500);
        });
    });
    
    // Shopping Button
    $("#btn_shopping").click(function(e){
        e.preventDefault();
        $("div#mask").fadeIn().delay(1000).fadeOut(function(){
            $("#checkout_page").hide();
            $("#shopping_page").fadeIn();
            _load_cartlist(true); 
        });
    });
    // ===============================================================================
    
    
    /** Donatur Slides **/
    // =============================================================================== 
    //  Store the slider in a local variable
    var flexslider;
        
    //  Tiny helper function to add breakpoints
    function getGridSize() {
        return  (window.innerWidth < 480) ? 2 :
                (window.innerWidth < 800) ? 4 :
                (window.innerWidth > 800) ? 8 : 8;
    }
    
    //  Check grid size on resize event
    $(window).resize(function() {
        var gridSize        = getGridSize();
        
        flexslider.vars.minItems        = gridSize;
        flexslider.vars.maxItems        = gridSize;
    });
    
    // Donatur Slide
    $(".donatur-slide").flexslider({
        animation: "slide",
        pauseOnHover: true,
        controlNav: false,
        directionNav: false,
        slideshow: false,
        itemWidth: 10,
        itemMargin: 0,
        minItems: getGridSize(),
        maxItems: getGridSize(),
        move: getGridSize(),
        useCSS: true,
        start: function (t) {
            flexslider = t;
            
            if (t.pagingCount < getGridSize()) {
                $('[data-control=".donatur-slide"]').hide()
            }
        },
        after: function(slider){
            var slide           = slider.data('flexslider'); slide.resize();
        }
    });
    // ===============================================================================
    
    
    /** Images Gallery **/
    // ===============================================================================
    // Load Images Gallery List
    function _load_images( url ){
        var url     = typeof(url) != 'undefined' ? url : 'gallery/imagedata';
        var el      = $("#image_gallery");
                
        $.ajax({
            type: "GET",
            data: {},
            url: url,
            beforeSend: function (){
                $("div#mask").fadeIn();
            },
            success: function( response ){
                response = $.parseJSON(response);
                $("div#mask").fadeOut();
                
                if(response.images != ""){
                    $(el).hide().append(response.images).fadeIn("slow");
                    $("#image_showmore").attr('href',response.pagination);
                    
                    // Load Image Gallery Colorbox
                    $('.image_gallery').colorbox({ 
                        rel:'image_gallery',
                        width: "80%",
                        height: "80%",
                        opacity: 0.6, 
                    });
                    
                    // Load Image Gallery Title Colorbox
                    $('.image_gallery_ttl').colorbox({ 
                        rel:'image_gallery_ttl',
                        width: "80%",
                        height: "80%",
                        opacity: 0.6, 
                    });   
                }
                
                return false;
            }
        });
    }
    
    // Event Show More Click
    $("#image_showmore").click(function(e){
        e.preventDefault();
        $('html, body').animate({ scrollTop: $("#image_showmore").offset().top - 100}, 500);
        
        var url = $(this).attr("href");
        _load_images( url );
    });
    // ===============================================================================
});